#include "linkqueue.h"

queue *init_queue(void)
{
    queue *q = calloc(1, sizeof(queue));
    if(q != NULL)
    {
        q->front = NULL;
        q->rear  = NULL;
        q->size  = 0;
    }
    return q;
}

list new_node(int data)
{
    list newnode = calloc(1, sizeof(node));
    if(newnode != NULL)
    {
        newnode->data = data;
        newnode->next = NULL;
    }
    return newnode;   
}

// newnode入队，排到队尾
bool en_queue(queue *q, list newnode)
{
    if(is_empty(q))
    {
        q->front = q->rear = newnode;
    }
    else
    {
        q->rear->next = newnode;
        q->rear = newnode;
    }

    q->size++;
    return true;
}

bool out_queue(queue *q, list *k)
{
    if(is_empty(q))
        return false;

    *k = q->front;
    q->front = q->front->next;
    (*k)->next = NULL;
    q->size--;

    if(q->size == 0)
        q->rear = NULL;

    return true;
}

bool front_queue(queue *q, list *k) // 取队头元素
{
    if(is_empty(q))
        return false;

    *k = q->front;
    return true;
}

bool is_empty(queue *q)
{
    return q->size == 0;
}
void destroy_init(queue *q) // 将队列恢复初始状态
{

}
void destroy(queue *q)
{}
void show(queue *q)
{
    if(is_empty(q))
        return;

    for(list p=q->front; p!=NULL; p=p->next)
    {
        printf("%d\t", p->data);
    }
    printf("\n");
}